package com.saascloud.openfeign.config;

import com.saascloud.openfeign.core.SaasFeignDecoder;
import com.saascloud.openfeign.core.SaasFeignHeaderInterceptor;
import com.saascloud.openfeign.core.SassFeignErrorDecoder;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.codec.ErrorDecoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;

/**
 * @Author: liangxu
 * @Contact: one_dream@aliyun.com
 * @Date: 2022/5/30 14:20
 * @Version: 1.0
 * @Description:
 */
public class FeignAutoConfig {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    public Encoder feignEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }

    @Bean
    @ConditionalOnProperty(name = "saas.feign.restful", havingValue = "true")
    public Decoder feignDecoder() {
        return new SaasFeignDecoder();
    }

    @Bean
    public ErrorDecoder feignErrorDecoder() {
        return new SassFeignErrorDecoder();
    }

    @Bean
    public SaasFeignHeaderInterceptor saasFeignHeaderInterceptor() {
        return new SaasFeignHeaderInterceptor();
    }
}
